import { Toast } from 'vant'
import { playSuccess, playError, playPapa, playWater, playRing, playDingding } from '../audio/webAudio'
import { iswap } from '@ve/utils/pdaUtils'
import trim from 'lodash/trim'
import { isEmpty } from 'lodash'
export default function msgFoucsAudio({ vm, action, done, callbackData }) {
  console.log('王月琪整合功能msgFoucsAudio', vm, action.extension)
  //操作1----->消息提示
  const useLastData = action.extension.useLastData.value
  const message = action.extension.message.value
  const staticList = message.split(/#{.*?}/gi)
  const variableList = message.match(/#{(.+?)\}/g)
  console.log('message', message, vm)
  let messageContent = ''
  if (useLastData) {
    // 如果使用上次任务数据，则通过key获取消息内容
    if (message.indexOf('->') !== -1) {
      const fieldList = message.split('->')
      messageContent = callbackData
      fieldList.forEach((fieldItem) => {
        // 如果没有后续值则不继续往下取值
        if (messageContent[trim(fieldItem)] === undefined) return
        messageContent = messageContent[trim(fieldItem)]
      })
    } else {
      messageContent = callbackData[message]
    }
  } else {
    if (variableList && variableList.length) {
      // 如果有配置变量 则先获取变量值
      variableList.forEach((variableItem, index) => {
        let variableContent = ''
        variableItem = trim(variableItem.slice(2, -1))
        if (variableItem.indexOf('->') !== -1) {
          const fieldList = variableItem.split('->')
          variableContent = callbackData
          fieldList.forEach((fieldItem) => {
            // 如果没有后续值则不继续往下取值
            if (variableContent[trim(fieldItem)] === undefined) return
            variableContent = variableContent[trim(fieldItem)]
          })
        } else {
          variableContent = callbackData[variableItem]
        }
        staticList.splice(index * 2 + 1, 0, variableContent)
      })
      messageContent = staticList.join('')
    } else {
      messageContent = message
    }
  }
  if (!isEmpty(messageContent)) {
    if (messageContent && messageContent.includes('<br/>')) {
      messageContent = messageContent.replace('<br/>', '')
    }
    Toast(messageContent)
  }

  // 操作2----->声音提示
  let type = action.extension.audioType.value
  if (!isEmpty(type)) {
    if (iswap() == 'WEB') {
      console.log('web方法', iswap())
      switch (type) {
        case 'success':
          playSuccess()
          break
        case 'error':
          playError()
          break
        case 'water':
          playWater()
          break
        case 'papa':
          playPapa()
          break
        case 'ring':
          playRing()
          break
        case 'ding':
          playDingding()
          break
        default:
          console.log('NoMusicType!')
          break
      }
      // type == 'OK' ? playSuccess() : playError()
    } else {
      console.log('安卓原生方法', iswap())
      window.$CIMC.playMusic(type, '')
    }
  }

  // 操作3----->光标聚焦
  let moduleId = action.extension.formItemSelect.value //获取选中得字段

  if (!isEmpty(moduleId)) {
    const formItem = vm.vuiComponents[moduleId] //获取选中得组件

    console.log('进来啦-----', formItem, moduleId)
    // 批次属性首次光标聚焦或者下拉弹起来
    if (moduleId[0].indexOf('uiPdaBatch') != -1) {
      const arrSelect = ['uiPdaTimePicker', 'uiPdaSelect']
      const arrInput = ['uiPdaInput', 'uiPdaDateTimeInput']
      // _module  获得批次属性第一个需要光标停留的组件实列
      let _module = vm.vuiComponents[formItem.self.focusIdArr[0]._id]
      // 光标聚焦时候下拉和时间组件弹起来
      // _module.self.showPicker = false
      if (arrSelect.indexOf(_module.self.getInfo.type) != -1) {
        _module.self.$closeModel()
      }
      // 光标聚焦时候输入框聚焦
      if (arrInput.indexOf(_module.self.getInfo.type) != -1) {
        _module && _module.ref && _module.ref.blur() //组件失去焦点
        setTimeout(() => {
          _module && _module.ref && _module.ref.focus() //组件聚焦
        }, 2)
      }
      done(null, {})
      return
    }
    let itemType = formItem.self.info.type
    if (itemType === 'uiPdaButton') {
      formItem.ref.$el.blur() //组件失去焦点
      setTimeout(() => {
        formItem.ref.$el.focus() //组件聚焦
      }, 2)
    } else {
      formItem && formItem.ref && formItem.ref.blur() //组件失去焦点
      setTimeout(() => {
        formItem && formItem.ref && formItem.ref.focus() //组件聚焦
      }, 2)
    }

    setTimeout(() => {
      let system = navigator.userAgent
      //如果为安卓端，光标聚焦后隐藏输入法
      if (system.indexOf('Android') > -1 || system.indexOf('Linux') > -1) {
        window.$CIMC.hideBoard()
      }
    }, 2.1)
  }
  done(null, {})
}
